package com.xiang.sql.service;

import java.util.ArrayList;
import java.util.List;

import jakarta.annotation.Resource;

import org.springframework.stereotype.Service;

import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiang.api.Util;
import com.xiang.sql.entity.User;
import com.xiang.sql.StaticSql;
import com.xiang.sql.entity.Goods;
import com.xiang.sql.entity.Stock;
import com.xiang.sql.mapper.StockMapper;

/**
 * 库存 服务实现类
 * 
 * @author mac-xiang
 */
@Service
public class StockService extends ServiceImpl<StockMapper, Stock> {
  @Resource
  StockMapper mapper;

  private QueryWrapper<Stock> readWrapper(JSONObject p, User per) {
    QueryWrapper<Stock> ret = Util.getQuery(p, Stock.class, "a");
    if (per != null && !per.per("库存查询")) {
      // ret.eq("`gid`", per.gid);
    }
    return ret;
  }

  private List<Stock> getList(QueryWrapper<Stock> q) {
    return mapper.selectList2(q);
  }

  private List<Stock> getList(JSONObject p, User per) {
    return getList(readWrapper(p, per));
  }

  private final static String limit = " LIMIT %d,%d";

  private Page<Stock> getPage(Page<Stock> page, JSONObject p, User per) {
    QueryWrapper<Stock> q = readWrapper(p, per);
    page.setTotal(mapper.count(q));
    List<Stock> d = getList(
        readWrapper(p, per).last(String.format(limit, page.getCurrent() - 1, page.getSize())));

    page.setRecords(d);
    return page;
  }

  public Stock create(JSONObject p, User per) {
    if (per != null && per.per("库存创建")) {
      Stock entity = Util.getEntity(p, Stock.class);
      entity.createTime = null;
      entity.sum = 0;
      // entity.create = per.id;
      mapper.insert(entity);
      return entity;
    }
    throw new RuntimeException("没有权限");
  }

  public int update(JSONObject p, User per) {
    if (per != null && per.per("库存修改")) {
      Stock entity = Util.getEntity(p, Stock.class);
      QueryWrapper<Stock> qw = Util.getQuery(p, Stock.class);
      if (this.count(qw) > 10) {
        throw new RuntimeException("单次修改数量超过10条");
      }
      // entity.create = null;
      return mapper.update(entity, qw);
    }
    throw new RuntimeException("没有权限");
  }

  public Object read(JSONObject p, User per) {
    Page<Stock> page = Util.getPage(p, Stock.class);
    return page == null ? getList(p, per) : getPage(page, p, per);
  }

  public int delete(JSONObject p, User per) {
    if (per != null && per.per("库存删除")) {
      QueryWrapper<Stock> q = Util.getQuery(p, Stock.class);
      if (this.count(q) > 10) {
        throw new RuntimeException("删除数据的数量不能超过10条");
      }
      return mapper.delete(q);
    }
    throw new RuntimeException("没有权限");
  }

  public long count(JSONObject p, User per) {
    return this.count(readWrapper(p, per));
  }

  public boolean exist(String id) {
    return mapper.selectCount(new QueryWrapper<Stock>().eq("id", id)) > 0;
  }

  public Stock _getByGG(String gid, String goods) {
    Stock stock = mapper.selectOne(new QueryWrapper<Stock>().eq("`gid`", gid).eq("`goods`", goods));
    if (stock == null) {
      stock = new Stock(gid, goods);
      mapper.insert(stock);
    }
    return stock;
  }

  public String _getIdByGG(String gid, String goods) {
    Stock s = _getByGG(gid, goods);
    if (s != null) {
      return s.id;
    }
    return null;
  }

  public Stock getFullByGG(String gid, String goods) {
    List<Stock> r = mapper.selectList2(new QueryWrapper<Stock>().eq("a.`gid`", gid).eq("a.`goods`", goods));
    if (r == null || r.size() == 0) {
      mapper.insert(new Stock(gid, goods));
      r = mapper.selectList2(new QueryWrapper<Stock>().eq("a.`gid`", gid).eq("a.`goods`", goods));
    }
    return r.get(0);
  }

  public List<Stock> other(JSONObject p, User per) {
    List<String> gids = new ArrayList<>();
    mapper.selectList(new QueryWrapper<Stock>().eq("`gid`", per.gid).select("`goods`")).forEach(e -> {
      gids.add(e.goods);
    });
    List<Stock> ret = new ArrayList<>();
    QueryWrapper<Goods> qw = new QueryWrapper<Goods>().select("id");
    if (gids.size() > 0) {
      qw.notIn("id", gids);
    }
    StaticSql.mapper.goods.selectList(qw).forEach(g -> {
      ret.add(new Stock(per.gid, g.id));
    });
    return ret;
  }
}
